package data;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

public class Forum {
	private int _id;
	private String _name;
	private HashMap <Integer, SubForum> _subForumColection;
	private Member _admin;
	private Integer _subForumIndex;
	
	public static Forum forum;
	
	public static Forum getForum()
	{
		if (forum == null)
			forum = new Forum(1, "genral", new Member("noName", 0));  //need to change according to the method which provides USER ID
		return forum;
	}
	
	public SubForum getSubForum(Integer index)
	{
		return this._subForumColection.get(index);
	}
	
	
	public Forum(int id, String name, Member admin) {
		super();
		_id = id;
		_name = name;
		_subForumColection= new HashMap<Integer, SubForum>();
		_subForumIndex=1;
		_admin=admin;
	}
	
	public String get_name() {
		return _name;
	}
	public void set_name(String name) {
		_name = name;
	}
	public int get_id() {
		return _id;
	}
	public HashMap<Integer, SubForum> get_subForumColection() {
		return _subForumColection;
	}

	public Member get_admin() {
		return _admin;
	}
/**
 * method which set the admin for the Forum
 * @param admin member whos going to be admin
 */
	public void set_admin(Member admin) {
		this._admin.set_isAdmin(false);
		_admin = admin;
		this._admin.set_isAdmin(true);
	}
	/**
	 * the method updates the moderator- DO NOT update the moderator again
	 * @param topic topic of sub-forum
	 * @param moderator moderator
	 * @return index of the new sub-forum
	 */
	public Integer addSubForum(String topic, Member moderator)
	{
		Integer index = this.get_subForumIndex();
		SubForum tempSubForum = new SubForum(index, topic, this, moderator);
		this._subForumColection.put(index, tempSubForum);
		moderator.addSubforumAsModerator(index);
		return index;
	}
	/**
	 * the method updates the moderator- DO NOT update the moderator again
	 * @param topic topic of sub-forum
	 * @param moderators map of moderators
	 * @return index of the new sub-forum
	 */
	public Integer addSubForum(String topic, HashMap<Integer, Member> moderators)
	{
		Integer index = this.get_subForumIndex();
		SubForum tempSubForum = new SubForum(index, topic, this, moderators);
		this._subForumColection.put(index, tempSubForum);
		Set<Entry<Integer, Member>> set = moderators.entrySet();
		for(Entry<Integer, Member> moderator : set)
			 moderator.getValue().addSubforumAsModerator(index);
		return index;
	}

	public synchronized Integer get_subForumIndex() {
		Integer ans = new Integer(this._subForumIndex);
		_subForumIndex++;
		return ans;
	}
/**
 * method which check if the user is permit to add new sub-forum to forum
 * @param userName member name
 * @return true if succeed false otherwise.
 */
	public boolean isPremitedToAddSubForum(String userName) {
		if (this.forum.get_admin().get_name().equals(userName)) 
			return true;
		return false;
	}
	/**
	 * method that returns all possible moderators for a new forum
	 * @return vector of all users - possible moderators
	 */
	public Vector<Member> getPossibleModeratorForNewForum() {
		 Map<String, Member> possibleModerator = MemberDataBase.getMembersDataBase().getUsers();
		 Set<Entry<String, Member>> set = possibleModerator.entrySet();
		 Vector<Member> possibleModeratorVec=new Vector<Member>();
		 for(Entry<String, Member> moderator : set)
			 possibleModeratorVec.add(moderator.getValue());
		return possibleModeratorVec;
	}
    /**
     * method that returns all possible moderators for a specific subForum
     * @param subForumInt - sub forum index
     * @return all possible moderators for forum - members that published more than 5 posts
     */
	public Vector<Member> getPossibleModeratorForForum(Integer subForumInt) {
		if (!this._subForumColection.containsKey(subForumInt))
		   return null;
		Vector<Member> possibleModeratorsVec = new Vector<Member>();
		Map<String, Member> users = MemberDataBase.getMembersDataBase().getUsers();
		for(Entry<String, Integer> temp : this._subForumColection.get(subForumInt).get__MapUserToNumMessages().entrySet()){
			if (temp.getValue() > 4)
				possibleModeratorsVec.add(users.get(temp.getKey()));
		}
		return possibleModeratorsVec;
	}
	/**
	 * method that delete a sub forum. the method updates the moderator- DO NOT update the moderator again
	 * @param userId - the userID of the man who wants to delete the sub forum
	 * @param subForumID - number of sub forum that need to  be deleted
	 */
	
	public Boolean deleteSubForum(int userId, int subForumID) {
		if(!_admin.get_id().equals(userId))
			return false;
		if (!this._subForumColection.containsKey(subForumID))
			return false;
		HashMap<Integer, Member> moderators = _subForumColection.get(subForumID).get_moderatorCollection();
		 Set<Entry<Integer, Member>> set = moderators.entrySet();
		 for(Entry<Integer, Member> moderator : set)
			 moderator.getValue().removeSubforumAsModerator(subForumID);
		 this._subForumColection.remove(subForumID);
		 return true;
	}
}
